package com.atguigu.order.orders.web;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.order.orders.ProductPmsEntity.PmsCategoryEntity;
import com.atguigu.order.orders.entity.OmsOrderEntity;
import com.atguigu.order.orders.service.OmsOrderService;
import com.atguigu.order.vo.OrderConfirmVo;
import com.atguigu.order.vo.OrderSubmitVo;
import com.atguigu.order.vo.SubmitOrderResponseVo;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;


/**
 * 订单
 *
 * @author qhw
 * @email qhw@gmail.com
 * @date 2021-08-03 19:49:59
 */
@EnableRedisHttpSession
@Controller
public class OrderWebController {

    @Autowired
    OmsOrderService omsOrderService;

   @GetMapping("/{page}.html")
    public String page(@PathVariable("page") String page){
       return page;
   }


    @GetMapping("toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {


       //定义方法查找对应的地址以及item信息
        OrderConfirmVo orderConfirmVo =  omsOrderService.toTrade();
        model.addAttribute("orderConfirmData",orderConfirmVo);
        return "confirm";
    }


    /**
     * 提交订单
     * @param model
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @PostMapping("submitOrder")
    public String submitOrder(Model model,OrderSubmitVo submitVo) throws ExecutionException, InterruptedException {
        submitVo.setAddrId(2333L);//将地址的id写死
        SubmitOrderResponseVo  submitOrderResponseVo = omsOrderService.submitOrder(submitVo);
        if(submitOrderResponseVo.getCode()!=0){
            return "redirect:http://order.gulimail.com/toTrade";
        }
        //定义方法查找对应的地址以及item信息
        model.addAttribute("submitOrderResp",submitOrderResponseVo);
        return  "pay";
    }

}
